/**
 * @file skoll.tools.autocomplete.js
 * @author Alejandro Dario Simi
 * @date $Date: 2014-05-04 01:33:52 +0000 (Sun, 04 May 2014) $
 *
 * $Id: skoll.tools.autocomplete.js 111 2014-05-04 01:33:52Z daemonraco@gmail.com $
 * $URL: http://wcomix.googlecode.com/svn/trunk/skins/Skoll/js/skoll.tools.autocomplete.js $
 */

/**
 * Skoll loading screen.
 */
Skoll.tools.Autocomplete = {
	//
	// Protected Properties.
	_autoTransID: 0,
	//
	// Public methods.
	bindClasses: function() {
		$("input[type='text'].Skoll-autocomplete-collections").each(function() {
			Skoll.tools.Autocomplete.bindForCollections(this);
		});
		$("input[type='text'].Skoll-autocomplete-directories").each(function() {
			Skoll.tools.Autocomplete.bindForDirectories(this);
		});
		$("input[type='text'].Skoll-autocomplete-searches").each(function() {
			Skoll.tools.Autocomplete.bindForSearches(this);
		});
		$("input[type='text'].Skoll-autocomplete-tags").each(function() {
			Skoll.tools.log($(this).attr("id"));
			Skoll.tools.Autocomplete.bindForTags(this);
		});
	},
	bindForCollections: function(obj) {

	},
	bindForDirectories: function(obj) {
		$(obj).bind("keydown", function(event) {
			if (event.keyCode === $.ui.keyCode.TAB && $(this).data("autocomplete").menu.active) {
				Skoll.tools.preventDefault(event);
			}
		}).autocomplete({
			source: function(request, response) {
				var pattern = request.term.split(/,\s*/).pop();
				if (pattern.length == 0) {
					return false;
				}

				Skoll.tools.log("Skoll.tools.Autocomplete.bindForDirectories(): Asking for pattern '" + pattern + "'");
				Skoll.tools.Autocomplete._autoTransID++;
				Skoll.tools.log("Skoll.tools.Autocomplete.bindForDirectories(): Sending transaction '" + Skoll.tools.Autocomplete._autoTransID + "'");
				$.ajax({
					dataType: "json",
					url: "service.php?service=directories:pattern&group=" + GROUP_ID + "&pattern=" + pattern + "&transaction=" + Skoll.tools.Autocomplete._autoTransID,
					type: "GET",
					data: {
					},
					success: function(data) {
						Skoll.tools.log("Skoll.tools.Autocomplete.bindForDirectories(): Receiving transaction '" + data.transaction + "'");
						if (data.transaction == Skoll.tools.Autocomplete._autoTransID) {
							Skoll.tools.log("Skoll.tools.Autocomplete.bindForDirectories(): Setting results from transaction '" + data.transaction + "'");
							if (data.status) {
								var availableDirs = Array();
								data.data.forEach(function(item) {
									availableDirs.push(item.name);
								});
								Skoll.tools.log("Skoll.tools.Autocomplete.bindForDirectories(): directory received: '" + availableDirs.length + "'");
								response(availableDirs);
							} else {
								Skoll.tools.log("Skoll.tools.Autocomplete.bindForDirectories(): [" + data.error.code + "] " + data.error.message);
							}
						}
					},
					error: function(data) {
						Skoll.tools.log("Skoll.tools.Autocomplete.bindForDirectories(): Error " + data);
					}
				});
			},
			focus: function() {
				// prevent value inserted on focus
				return false;
			},
			select: function(event, ui) {
				var terms = this.value.split(/,\s*/);
				// remove the current input
				terms.pop();
				// add the selected item
				terms.push(ui.item.value);
				// add placeholder to get the comma-and-space at the end
				terms.push("");
				this.value = terms.join(", ");
				return false;
			}
		});
	},
	bindForSearches: function(obj) {

	},
	bindForTags: function(obj) {
		$(obj).bind("keydown", function(event) {
			if (event.keyCode === $.ui.keyCode.TAB && $(this).data("autocomplete").menu.active) {
				Skoll.tools.preventDefault(event);
			}
		}).autocomplete({
			source: function(request, response) {
				var pattern = request.term.split(/,\s*/).pop();
				if (pattern.length == 0) {
					return false;
				}

				Skoll.tools.log("Skoll.tools.Autocomplete.bindForTags(): Asking for pattern '" + pattern + "'");
				Skoll.tools.Autocomplete._autoTransID++;
				Skoll.tools.log("Skoll.tools.Autocomplete.bindForTags(): Sending transaction '" + Skoll.tools.Autocomplete._autoTransID + "'");
				$.ajax({
					dataType: "json",
					url: "service.php?service=tags:pattern&group=" + GROUP_ID + "&pattern=" + pattern + "&transaction=" + Skoll.tools.Autocomplete._autoTransID,
					type: "GET",
					data: {
					},
					success: function(data) {
						Skoll.tools.log("Skoll.tools.Autocomplete.bindForTags(): Receiving transaction '" + data.transaction + "'");
						if (data.transaction == Skoll.tools.Autocomplete._autoTransID) {
							Skoll.tools.log("Skoll.tools.Autocomplete.bindForTags(): Setting results from transaction '" + data.transaction + "'");
							if (data.status) {
								var availableTags = Array();
								data.data.forEach(function(item) {
									availableTags.push(item.name);
								});
								Skoll.tools.log("Skoll.tools.Autocomplete.bindForTags(): tags received: '" + availableTags.length + "'");
								response(availableTags);
							} else {
								Skoll.tools.log("Skoll.tools.Autocomplete.bindForTags(): [" + data.error.code + "] " + data.error.message);
							}
						}
					},
					error: function(data) {
						Skoll.tools.log("Skoll.tools.Autocomplete.bindForTags(): Error " + data);
					}
				});
			},
			focus: function() {
				// prevent value inserted on focus
				return false;
			},
			select: function(event, ui) {
				var terms = this.value.split(/,\s*/);
				// remove the current input
				terms.pop();
				// add the selected item
				terms.push(ui.item.value);
				// add placeholder to get the comma-and-space at the end
				terms.push("");
				this.value = terms.join(", ");
				return false;
			}
		});
	}
};
$(document).ready(function() {
	Skoll.tools.Autocomplete.bindClasses();
});